class ReactX {
  eventName: string | object;

  constructor() {
    //事件名
    this.eventName = {};
  }
  emit = (name, data) => {
    const cbList = this.eventName[name];
    cbList &&
      cbList.length &&
      cbList.forEach((item) => {
        item && item(data);
      });
  };
  listen = (name, cb) => {
    const { eventName } = this;
    if (!(eventName[name] && eventName[name].length)) {
      eventName[name] = [];
    }
    eventName[name].push(cb);
  };
  remove(name, cb) {
    if (cb) {
      let cbs = this.eventName[name];
      cbs = cbs.filter((item) => item !== cb);
      this.eventName[name] = cbs;
    } else {
      this.eventName[name] = null;
      delete this.eventName[name];
    }
  }
}

export default new ReactX();
